//
//  NewsCommentCell.m
//  SportSocial
//
//  Created by yull on 15/1/5.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "NewsCommentCell.h"
#import "NSString+Size.h"
#import <UIImageView+WebCache.h>
@implementation NewsCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        
        CGRect rect = CGRectMake(10, 10, 40, 40);
        self.userIconImageView = [[UIImageView alloc] initWithFrame:rect];
        self.userIconImageView.layer.cornerRadius = rect.size.width/2;
        self.userIconImageView.layer.masksToBounds = YES;
        self.userIconImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:self.userIconImageView];
        
        rect.origin.x = self.userIconImageView.right + 10;
        rect.size.width = self.contentView.width - rect.origin.x;
        rect.size.height = 25;
        self.userNameLabel = [[UILabel alloc] initWithFrame:rect];
        self.userNameLabel.backgroundColor = [UIColor clearColor];
        self.userNameLabel.font = [UIFont systemFontOfSize:18.0f];
        self.userNameLabel.textColor = [UIColor blackColor];
        self.userNameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self.contentView addSubview:self.userNameLabel];
        
        rect.origin.y = self.userNameLabel.bottom;
        rect.size.height = self.userIconImageView.height - self.userNameLabel.height;
        self.timeLabel = [[UILabel alloc] initWithFrame:rect];
        self.timeLabel.backgroundColor = [UIColor clearColor];
        self.timeLabel.font = [UIFont systemFontOfSize:15.0f];
        self.timeLabel.textColor = [UIColor grayColor];
        self.timeLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self.contentView addSubview:self.timeLabel];
        
        rect = CGRectMake(self.userIconImageView.left, self.userIconImageView.bottom + 5, self.contentView.width - self.userIconImageView.left * 2, 0);
        self.commentLabel = [[UILabel alloc] initWithFrame:rect];
        self.commentLabel.backgroundColor = [UIColor clearColor];
        self.commentLabel.font = [UIFont systemFontOfSize:14.0f];
        self.commentLabel.textColor = [UIColor blackColor];
        self.commentLabel.numberOfLines = 0;
        [self.contentView addSubview:self.commentLabel];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapClick:)];
        [self.userIconImageView addGestureRecognizer:tapGesture];
        
    }
    return self;
}

+ (CGFloat)calculateCommentHeight:(NSString *)commentText
{
    CGSize size = CGSizeMake(SCREEN_HEIGHT - 20, MAXFLOAT);
    CGFloat height = [commentText getSizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedSize:size].height;
    return 70.0f + height;
}

- (void)drawCellWithComment:(NewsComment *)newsComment
{
    self.comment = newsComment;
    
    [self.userIconImageView sd_setImageWithURL:[NSURL URLWithString:newsComment.user_icon] placeholderImage:[UIImage imageNamed:@"zc_photo_bg"]];
    
    self.userNameLabel.text = newsComment.user_name;
    
    self.timeLabel.text = newsComment.created_date;
    
    self.commentLabel.text = newsComment.comment;
    
    CGSize size = CGSizeMake(SCREEN_HEIGHT - 20, MAXFLOAT);
    CGFloat height = [newsComment.comment getSizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedSize:size].height;
    self.commentLabel.height = height;
}

- (void)onTapClick:(id)gesture
{
    if ([self.delegate respondsToSelector:@selector(onHeadImageClick:)]) {
        [self.delegate onHeadImageClick:self.comment];
    }
}
@end
